import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


class View(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack(fill=tk.BOTH, expand=True)
        self.create_widgets()

    def create_widgets(self):
        # 创建和布置Tkinter控件
        self.load_button = tk.Button(self, text="Load MF4", command=self.ask_open_file)
        self.load_button.pack()

        self.report_button = tk.Button(self, text="Generate Report", command=self.on_generate_report)
        self.report_button.pack()

        self.show_plot_button = tk.Button(self, text="Show Plot", command=self.on_show_plot)
        self.show_plot_button.pack()

        self.convertAllMF42MAT = tk.Button(self, text="Convert all to MAT", command=self.ask_open_dir)
        self.convertAllMF42MAT.pack()
        # 创建一个带滚动条的Canvas
        self.canvas = tk.Canvas(self)
        self.scrollbar = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.scrollbar.set)

        self.scrollbar.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)

        # 在Canvas上创建一个Frame用于放置图形
        self.plot_frame = tk.Frame(self.canvas)
        self.canvas.create_window((0, 0), window=self.plot_frame, anchor="nw")

        self.plot_frame.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))

    def ask_open_file(self):
        file_path = filedialog.askopenfilename(filetypes=[("MF4 Files", "*.mf4")])
        if file_path:
            self.on_load(file_path)
    def ask_open_dir(self):
        dir_path = filedialog.askdirectory()
        if dir_path:
            self.on_convert(dir_path)
    def on_load(self, file_path):
        pass

    def on_generate_report(self):
        pass

    def on_show_plot(self):
        pass
    def on_convert(self):
        pass
    def show_figure(self, fig):
        # 在plot_frame中显示图形
        canvas = FigureCanvasTkAgg(fig, master=self.plot_frame)
        canvas_widget = canvas.get_tk_widget()
        canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        canvas.draw()
